//
//  MapData.m
//  MapGridbyHong
//
//  Created by 컴011 on 1/18/14.
//  Copyright (c) 2014 handymap. All rights reserved.
//

#import "MapData.h"

@implementation MapData

@synthesize imageWidth, imageHeight;

-(void)setUIImage:(UIImage*)image
{
    //NSMutableArray *resultColor = [NSMutableArray array];
    CGImageRef imageRef = [image CGImage];
    imageWidth = CGImageGetWidth(imageRef);
    imageHeight = CGImageGetHeight(imageRef);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    mapDataArry = (unsigned char*) calloc(imageWidth * imageHeight * 4, sizeof(unsigned char));
    
    bytesPerPixel = 4;
    
    bytesPerRow = bytesPerPixel * imageWidth;
    
    NSUInteger bitsPerComponent = 8;
    
    CGContextRef context = CGBitmapContextCreate(mapDataArry, imageWidth, imageHeight,
                                                 
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    
    CGColorSpaceRelease(colorSpace);
    
    CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), imageRef);
    
    CGContextRelease(context);
}

-(unsigned char*)getRGBAImagePosition:(int)xp atY:(int)yp
{
    unsigned char* resultData = (unsigned char*) calloc(40, sizeof(unsigned char));
    resultData [0] = 1;
    for (int i=0; i<4; i++) {
        resultData[i] = mapDataArry[(bytesPerRow * yp) + xp * bytesPerPixel+i];
        
    }
    return resultData;

}
@end
